class Student:
	pass

# 动态绑定属性
s = Student()
s.name = "jim"

s2 = Student()

print(s.name)

# 动态给一个实例绑定方法
def ask(self, msg):
	self.msg = msg

from types import MethodType
s.ask = MethodType(ask, s)
s.ask("ask msg")
print(s.msg) # 输出ask msg


# 动态为类绑定方法

def sleep(self):
	print(self.name, "student sleeping....")

Student.sleep = sleep

s.sleep()
try:
	s2.sleep() # 这里为报错，实例没name属性
except Exception as ex:
	print("s2 ex:", ex)


class People:
	__slots__ = ("name","age")

	def __str__(self):
		return "people name:{0}, age:{1}".format(self.name, self.age)

# 通过__slots__对类绑定做限制,仅对当前类实例起作用，对继承的子类是不起作用的：
p = People()
p.name = "tom"
p.age = 12
try:
	p.address = "am" # 这里报错：
except Exception as ex:
	print("p ex", ex)

print(p)
